<?php

error_reporting(E_ALL);
header('Content-Type: text/html; charset=utf-8'); //网页编码
//debug(authcode('123456', 'ENCODE'));

import_user();

function import_user() {
    require_once __DIR__ . '/extend/idcardinfo.php';
    $ret = db()->query("select `name`,phone,email,`password`,idcard,idcard_type,sex,province,school,faculties,major,grade,graduyear,dateline from lcl_ztepy where sex!='' and province!='' and  email not like '%@zte%' ")->fetchAll(PDO::FETCH_ASSOC);
    $i = 0;
    foreach ($ret as $row) {
        $arr = array();
        $arr['username'] = $row['name'];
        $arr['email'] = $row['email'];
        $arr['password'] = authcode($row['password'], 'ENCODE');
        $arr['phone'] = $row['phone'];
        $arr['avatarstatus'] = "0";
        $arr['amount'] = "0";
        $arr['groupid'] = "1";
        $arr['regip'] = $arr['lastloginip'] = "127.0.0.1";
        $arr['regdate'] = $arr['lastlogintime'] = $row['dateline'];
        $arr['adminid'] = "0";
        $arr['timeoffset'] = "";
        $arr['status'] = 1;
        $arr['dateline'] = time();

        $fields = $values = array();
        foreach ($arr AS $key => $val) {
            $fields[] = '`' . $key . '`';
            $values[] = "'" . $val . "'";
        }

        $sql = ' INTO tky_member(' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';

        $dba = db('tickycms');
        $dba->exec($sql);
        $uid = $dba->lastInsertId();
        echo $i . ':' . $uid . ' ' . $sql . '<br>';
        $i++;
        if ($uid) {
            $arr = array();
            $arr['uid'] = $uid;
            $sex = "0";
            if ($row['sex'] == '男') {
                $sex = "1";
            } elseif ($row['sex'] == '女') {
                $sex = "2";
            }

            $idinfo = idcardinfo::CreditNoInfo($row['idcard'], '-');

            if ($row['idcard_type'] == '身份证') {
                $vBirthday = $idinfo['birthday'];
                $arr['birthyear'] = substr($vBirthday, 0, 4);
                $arr['birthmonth'] = substr($vBirthday, 5, 2);
                $arr['birthday'] = substr($vBirthday, 8, 2);
                $arr['birthprovince'] = $idinfo['shenfen'];
                $arr['birthcity'] = '';
                $arr['birthdist'] = '';

                $arr['constellation'] = $idinfo['constellation'];
                $arr['zodiac'] = $idinfo['zodiac'];
                $arr['address'] = $idinfo['nativeplace'];
            } else {
                $arr['birthyear'] = '';
                $arr['birthmonth'] = '';
                $arr['birthday'] = '';
                $arr['constellation'] = '';
                $arr['zodiac'] = '';
            }

            $arr['realname'] = $row['name'];
            $arr['gender'] = $sex;
            $arr['idcardtype'] = $row['idcard_type'];
            $arr['idcard'] = $row['idcard'];
            $arr['zipcode'] = '';
            $arr['nationality'] = '中国';

            $arr['resideprovince'] = $row['province'];
            $arr['residecity'] = '';
            $arr['residedist'] = '';
            $arr['residecommunity'] = '';
            $arr['residesuite'] = '';
            $arr['graduateschool'] = $row['school'];
            $arr['company'] = '';
            $arr['education'] = '';
            $arr['occupation'] = '';
            $arr['position'] = '';
            $arr['revenue'] = '';
            $arr['affectivestatus'] = '';
            $arr['lookingfor'] = '';
            $arr['bloodtype'] = '';
            $arr['height'] = '';
            $arr['weight'] = '';
            $arr['alipay'] = '';
            $arr['icq'] = '';
            $arr['qq'] = '';
            $arr['yahoo'] = '';
            $arr['msn'] = '';
            $arr['taobao'] = '';
            $arr['site'] = '';
            $arr['bio'] = '';
            $arr['interest'] = '';

            $fields = $values = array();
            foreach ($arr AS $key => $val) {
                $fields[] = '`' . $key . '`';
                $values[] = "'" . $val . "'";
            }
            $sql = ' REPLACE INTO tky_member_profile(' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
            db('tickycms')->exec($sql);
            echo $sql . '<br>';
        }
    }
    echo '导入完成';
}

function db($dbname = 'challenge') {
    $dsn = "mysql:host=127.0.0.1;port=3306;dbname=" . $dbname;
    $db = new PDO($dsn, 'root', '123456');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); //使用缓冲查询，仅mysql有效
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); //启用预处理语句的模拟
    $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); //强制列名小写
    $db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); //指定数据库返回的NULL值在php中对应的数值
    $db->setAttribute(PDO::ATTR_AUTOCOMMIT, 1); //开启自动提交
    $db->exec('SET NAMES utf8');
    echo "==当前数据库:" . $dsn . "===================<br>";
    return $db;
}

/**
 * 加密解密
 *  $string： 明文 或 密文
 * $operation：DECODE表示解密,其它表示加密
 *  $key： 密匙
 * $expiry：密文有效期
 */
function authcode($string, $operation = 'DECODE', $key = 'luomingui', $expiry = 0) {
    $ckey_length = 4;
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}

function debug($var = null, $vardump = false) {
    echo '<pre>';
    $vardump = empty($var) ? true : $vardump;
    if ($vardump) {
        var_dump($var);
    } else {
        print_r($var);
    }
    echo '</pre>';
    exit();
}
